package com.standardoperationapp.operationplan;

import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.standardoperationapp.R;
import com.standardoperationapp.adapter.PointListAdapter;
import com.standardoperationapp.bean.CardPointBean;
import com.standardoperationapp.bean.CardPointStepBean;
import com.standardoperationapp.databinding.ActivityPlanPointListBinding;
import com.standardoperationapp.global.GsonUtils;
import com.standardoperationapp.global.SharedPreferencesUtils;
import com.standardoperationapp.net.OnSuccessAndFaultListener;
import com.standardoperationapp.net.OnSuccessAndFaultSub;
import com.standardoperationapp.net.OperationApi;
import com.standardoperationapp.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class PlanPointListActivity extends AppCompatActivity {
    private ActivityPlanPointListBinding binding;
    String mUserName;
    private PointListAdapter adapter;
    private List<CardPointBean.DataBean> dataList = new ArrayList();
    private int planId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_plan_point_list);
        mUserName = SharedPreferencesUtils.getSharedPreferencesData(this,"username");


        planId = getIntent().getIntExtra("planId", 0);

        adapter = new PointListAdapter(this, dataList);
        binding.pointListView.setAdapter(adapter);
        binding.pointListView.setOnItemClickListener((adapterView, view, i, l) -> {
            if (dataList.get(i).getImplementStatus().equals("2")) {
                ToastUtil.showToast("请勿重复操作");
                return;
            }
            getPointOperation(dataList.get(i));
        });
        initData(planId);
    }

    private void initData(int planId) {
        OperationApi.selectCardPointListById(planId, new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
            @Override
            public void onSuccess(Object result) {
                CardPointBean bean = GsonUtils.fromJson(result, CardPointBean.class);
                binding.tvTitle.setText(bean.getData().get(0).getPlanchargeUserIdName());
                dataList.clear();
                if (bean.getData() != null) {
                    dataList.addAll(bean.getData());
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFault(String errorMsg) {
                ToastUtil.showToast(errorMsg);
            }
        }));
    }

    private void getPointOperation(CardPointBean.DataBean dataBean) {
        OperationApi.selectPointOperateByPointId(Integer.parseInt(dataBean.getWorkPointId()), planId, dataBean.getImplementId(), new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
            @Override
            public void onSuccess(Object result) {
                CardPointStepBean bean = GsonUtils.fromJson(result, CardPointStepBean.class);
                if (bean.getData() != null && bean.getData().size()>0) {
                    Intent intent = new Intent(PlanPointListActivity.this, PointDetailActivity.class);
                    intent.putExtra("title", dataBean.getProjectName() + "+" + dataBean.getWorkPointName());
                    intent.putExtra("bean", bean);
                    intent.putExtra("planId", planId);
                    intent.putExtra("implementId", dataBean.getImplementId());
                    intent.putExtra("workPointId", Integer.parseInt(dataBean.getWorkPointId()));
                    startActivity(intent);
                }
            }

            @Override
            public void onFault(String errorMsg) {
                ToastUtil.showToast(errorMsg);
            }
        }));
    }
}